package com.inspur.edp.web.formmetadata.i18n.component.strategy.i18nresourcestrategy.CommonStrategy;

import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItem;
import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItemCollection;
import com.inspur.edp.web.common.utility.StringUtility;
import com.inspur.edp.web.formmetadata.i18n.I18nResourceItemManager;
import com.inspur.edp.web.formmetadata.i18n.component.ComponentUtility;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.i18nresourcestrategy.AbstractI18nResourceStrategy;
import com.inspur.edp.web.formmetadata.i18n.constant.I18nResourceConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class GridCommonStrategy extends AbstractI18nResourceStrategy {

    /**
     * 提取列表上下文参数
     *
     * @param i18nResourceItemBaseId
     * @param currentComponent
     * @return
     */
    protected I18nResourceItemCollection extractContextMenu(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        String contextMenuI18nResourceBaseId = ComponentUtility.getInstance().getType(currentComponent) + I18nResourceConstant.SECOND_LEVEL_DELIMITER + ComponentUtility.getInstance().getId(currentComponent) + I18nResourceConstant.SECOND_LEVEL_DELIMITER + "contextMenuItems" + I18nResourceConstant.SECOND_LEVEL_DELIMITER;
        ArrayList<HashMap<String, Object>> contextMenuItemList = ComponentUtility.getInstance().getDataGridContextMenu(currentComponent);
        if (contextMenuItemList != null) {
            contextMenuItemList.forEach(t -> extractContextMenuItem(i18nResourceItemBaseId, contextMenuI18nResourceBaseId, t, i18nResourceItemCollection));
        }
        return i18nResourceItemCollection;
    }

    /**
     * 提取列表上下文参数
     *
     * @param i18nResourceItemBaseId
     * @param currentComponent
     * @return
     */
    protected I18nResourceItemCollection extractContextColumns(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        String contextMenuI18nResourceBaseId = ComponentUtility.getInstance().getType(currentComponent) + I18nResourceConstant.SECOND_LEVEL_DELIMITER + ComponentUtility.getInstance().getId(currentComponent) + I18nResourceConstant.SECOND_LEVEL_DELIMITER + "columns" + I18nResourceConstant.SECOND_LEVEL_DELIMITER;
        ArrayList<HashMap<String, Object>> contextMenuItemList = ComponentUtility.getInstance().getColumns(currentComponent);
        if (contextMenuItemList != null) {

            //todo
            contextMenuItemList.forEach(t -> extractContextMenuItem(i18nResourceItemBaseId, contextMenuI18nResourceBaseId, t, i18nResourceItemCollection));
        }
        return i18nResourceItemCollection;
    }

    /**
     * 提取列表上下文参数中的具体项及其children
     *
     * @param i18nResourceItemBaseId
     * @param contextMenuResourceBaseId
     * @param contextMenuItem
     * @param contextMenuResourceCollection
     */
    private void extractContextMenuItem(String i18nResourceItemBaseId, String contextMenuResourceBaseId, HashMap<String, Object> contextMenuItem, I18nResourceItemCollection contextMenuResourceCollection) {
        String contextMenuItemId = ComponentUtility.getInstance().getId(contextMenuItem);
        String title = ComponentUtility.getInstance().getTitle(contextMenuItem);
        String resourceId = contextMenuResourceBaseId + contextMenuItemId;
        I18nResourceItem contextMenuI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, resourceId, title, title);
        this.addInCollection(contextMenuResourceCollection, contextMenuI18nResourceItem);

        //获取对应的children的值
        ArrayList<HashMap<String, Object>> contextMenuItemChildren = ComponentUtility.getInstance().getDataGridContextMenuItemChildren(contextMenuItem);
        if (contextMenuItemChildren != null) {
            contextMenuItemChildren.forEach(t -> this.extractContextMenuItem(i18nResourceItemBaseId, contextMenuResourceBaseId, t, contextMenuResourceCollection));
        }
    }

    /**
     * 提取farris-grid的 操作列   编辑、删除
     *
     * @param i18nResourceItemBaseId
     * @param currentComponent
     * @return
     */
    protected I18nResourceItemCollection ExtractComponentOperateButtonI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();

        String currentComponentType = ComponentUtility.getInstance().getType(currentComponent);
        String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
        if (StringUtility.isNullOrEmpty(currentComponentType)) {
            return null;
        }
        if(i18nResourceItemBaseId.equals("")){
//            if(currentComponent.containsKey("command")){
//                LinkedHashMap command = (LinkedHashMap) currentComponent.get("command");
//                if(command.containsKey("commands")){
//                    List<LinkedHashMap<String,Object>> commands= (List<LinkedHashMap<String, Object>>) command.get("commands");
//                    for(int i = 0 ; i<commands.size();i++){
//                        I18nResourceItem editI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, currentComponentId+"/commands/"+i, (String)commands.get(i).get("text"), (String)commands.get(i).get("text"));
//                        this.addInCollection(i18nResourceItemCollection, editI18nResourceItem);
//                    }
//                }
//            }
        }
        else {
            String operateEditButtonAtributeName = "OperateEditButton";
            String operateEditButtonAtributeValue = "编辑";
            // 使用 "/"作为分隔符
            String generatedOperateEditButtonComponentId = currentComponentType + "/" + currentComponentId + "/" + operateEditButtonAtributeName;
            I18nResourceItem editI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedOperateEditButtonComponentId, operateEditButtonAtributeValue, operateEditButtonAtributeValue);
            this.addInCollection(i18nResourceItemCollection, editI18nResourceItem);


            String operateDeleteButtonAtributeName = "OperateDeleteButton";
            String operateDeleteButtonAtributeValue = "删除";
            // 使用 "/"作为分隔符
            String generatedOperateDeleteButtonComponentId = currentComponentType + "/" + currentComponentId + "/" + operateDeleteButtonAtributeName;
            I18nResourceItem deleteI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedOperateDeleteButtonComponentId, operateDeleteButtonAtributeValue, operateDeleteButtonAtributeValue);
            this.addInCollection(i18nResourceItemCollection, deleteI18nResourceItem);


            String operateColumnAtributeName = "OperateColumn";
            String operateColumnAtributeValue = "操作";
            // 使用 "/"作为分隔符
            String generatedOperateColumnComponentId = currentComponentType + "/" + currentComponentId + "/" + operateColumnAtributeName;
            I18nResourceItem operateColumnI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedOperateColumnComponentId, operateColumnAtributeValue, operateColumnAtributeValue);
            this.addInCollection(i18nResourceItemCollection, operateColumnI18nResourceItem);
        }

        return i18nResourceItemCollection;
    }

    protected I18nResourceItemCollection ExtractComponentLineNumberTitleI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();

        String currentComponentType = ComponentUtility.getInstance().getType(currentComponent);
        String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
        if (StringUtility.isNullOrEmpty(currentComponentType) || StringUtility.isNullOrEmpty(currentComponentId)) {
            return null;
        }

        String lineNumberTitleAtributeName = ComponentUtility.getInstance().GetLineNumberTitleName(currentComponent);
        String lineNumberTitleAtributeValue = ComponentUtility.getInstance().getLineNumberTitle(currentComponent);

        // 使用 "/"作为分隔符

        String generatedComponentId = currentComponentType + "/" + currentComponentId + "/" + lineNumberTitleAtributeName;
        if(i18nResourceItemBaseId.equals("")){
            generatedComponentId=currentComponentId+"/"+lineNumberTitleAtributeName;
        }
        if (!StringUtility.isNullOrEmpty(lineNumberTitleAtributeValue)) {
            I18nResourceItem i18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId, lineNumberTitleAtributeValue, lineNumberTitleAtributeValue);
            this.addInCollection(i18nResourceItemCollection, i18nResourceItem);
        } else {
            I18nResourceItem i18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId, "", "");
            this.addInCollection(i18nResourceItemCollection, i18nResourceItem);
        }

        return i18nResourceItemCollection;
    }

    protected I18nResourceItemCollection ExtractHeaderGroupResourceItemCollection(String i18nResourceItemBaseId, String headerGroupI18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();

        headerGroupI18nResourceItemBaseId = headerGroupI18nResourceItemBaseId + ComponentUtility.getInstance().getHeaderGroupName(currentComponent) + I18nResourceConstant.SECOND_LEVEL_DELIMITER;

        ArrayList<HashMap<String, Object>> headerGroupItemCollection = ComponentUtility.getInstance().getHeaderGroup(currentComponent);

        for (HashMap<String, Object> headerGroupItem : headerGroupItemCollection) {
            if (!StringUtility.isNullOrEmpty(ComponentUtility.getInstance().getFieldRef(headerGroupItem)) || ComponentUtility.getInstance().getCandidateNodeFlag(headerGroupItem)) {
                continue;
            }

            String componentId = ComponentUtility.getInstance().getId(headerGroupItem);
            String componentName = ComponentUtility.getInstance().getCaption(headerGroupItem);

            I18nResourceItem i18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, headerGroupI18nResourceItemBaseId + componentId, componentName, componentName);
            this.addInCollection(i18nResourceItemCollection, i18nResourceItem);
        }

        return i18nResourceItemCollection;
    }

    protected I18nResourceItemCollection extractedColumns(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        String currentComponentType = ComponentUtility.getInstance().getType(currentComponent);
        String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
        List<HashMap<String,Object>> columns = (List<HashMap<String, Object>>) currentComponent.get("columns");
        for (HashMap<String, Object> column : columns) {
            String columnType = (String) column.get("type");
            String columnID = (String) column.get("id");

            String generatedComponentId = columnID;
            I18nResourceItem i18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId, (String) column.get("title"), (String) column.get("title"));
            this.addInCollection(i18nResourceItemCollection, i18nResourceItem);
            if(column.get("dataType").equals("number")){
                I18nResourceItem prefixI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId+"/formatter/prefix",(String)((LinkedHashMap<String,Object>) column.get("formatter")).get("prefix"), (String)((LinkedHashMap<String,Object>) column.get("formatter")).get("prefix"));
                I18nResourceItem suffixI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId+"/formatter/suffix",(String) ((LinkedHashMap<String,Object>) column.get("formatter")).get("suffix"), (String)((LinkedHashMap<String,Object>) column.get("formatter")).get("suffix"));
                this.addInCollection(i18nResourceItemCollection, prefixI18nResourceItem);
                this.addInCollection(i18nResourceItemCollection, suffixI18nResourceItem);
            }

            if(column.containsKey("formatter")&&column.get("formatter")!=null){
                LinkedHashMap<String,Object>formatter= (LinkedHashMap<String, Object>) column.get("formatter");
                if(formatter.containsKey("data")){
                    ArrayList<LinkedHashMap<String,Object>>data = (ArrayList<LinkedHashMap<String, Object>>) formatter.get("data");
                    for (int i = 0;i<data.size();i++){
                        I18nResourceItem formatterI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId+"/formatter/data/"+data.get(i).get("value"), (String) data.get(i).get("name"), (String)data.get(i).get("name"));
                        this.addInCollection(i18nResourceItemCollection, formatterI18nResourceItem);
                    }
                }
                if(formatter.containsKey("trueText")&&formatter.get("trueText")!=null){
                    I18nResourceItem trueI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId+"/formatter/trueText",(String)((LinkedHashMap<String,Object>) column.get("formatter")).get("trueText"), (String)((LinkedHashMap<String,Object>) column.get("formatter")).get("trueText"));
                    this.addInCollection(i18nResourceItemCollection, trueI18nResourceItem);
                }
                if(formatter.containsKey("falseText")&&formatter.get("falseText")!=null){
                    I18nResourceItem falseI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId+"/formatter/falseText",(String) ((LinkedHashMap<String,Object>) column.get("formatter")).get("falseText"), (String)((LinkedHashMap<String,Object>) column.get("formatter")).get("falseText"));
                    this.addInCollection(i18nResourceItemCollection, falseI18nResourceItem);
                }
                if(formatter.containsKey("prefix")&&formatter.get("prefix")!=null){
                    I18nResourceItem prefixI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId+"/formatter/prefix",(String)((LinkedHashMap<String,Object>) column.get("formatter")).get("prefix"), (String)((LinkedHashMap<String,Object>) column.get("formatter")).get("prefix"));
                    this.addInCollection(i18nResourceItemCollection, prefixI18nResourceItem);
                }
                if(formatter.containsKey("suffix")&&formatter.get("falseText")!="suffix"){
                    I18nResourceItem suffixI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId+"/formatter/suffix",(String) ((LinkedHashMap<String,Object>) column.get("formatter")).get("suffix"), (String)((LinkedHashMap<String,Object>) column.get("formatter")).get("suffix"));
                    this.addInCollection(i18nResourceItemCollection, suffixI18nResourceItem);
                }

            }
            //如果存在枚举情况
            if (column.containsKey("editor")&&column.get("editor")!=null) {
                HashMap<String, Object> editors = (HashMap<String, Object>) column.get("editor");
                if (!editors.isEmpty() && editors.containsKey("data")) {
                    List<Object> datas = (List<Object>) editors.get("data");

                    for (Object o : datas) {
                        HashMap<Object, Object> data = (HashMap<Object, Object>) o;
                        String childComponentId = generatedComponentId + "/editor/data/" + (data.get("value"));
                        I18nResourceItem footerButtonI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, childComponentId,( String)data.get("name"), (String)(data.get("name")));
                        this.addInCollection(i18nResourceItemCollection, footerButtonI18nResourceItem);

                    }
                }
                if(editors.containsKey("placeholder")&&editors.get("placeholder")!=null){
                    String childComponentId = generatedComponentId + "/placeholder";
                    I18nResourceItem footerButtonI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, childComponentId, (String)(editors.get("placeholder")), (String)(editors.get("placeholder")));
                    this.addInCollection(i18nResourceItemCollection, footerButtonI18nResourceItem);
                }
                if(editors.containsKey("dialog")&&editors.get("dialog")!=null){
                    LinkedHashMap<String,Object>dialog= (LinkedHashMap<String, Object>) editors.get("dialog");
                    String title= (String) dialog.get("title");
                    String childComponentId = generatedComponentId + "/dialog/title";
                    I18nResourceItem dialogI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, childComponentId, title, title);
                    this.addInCollection(i18nResourceItemCollection, dialogI18nResourceItem);

                }
                if(editors.containsKey("formatValidation")&&editors.get("formatValidation")!=null){

                    LinkedHashMap<String,Object> formatValidation= (LinkedHashMap<String, Object>) editors.get("formatValidation");
                    String message = (String) formatValidation.get("message");
                    String childComponentId = generatedComponentId +   "/formatValidation/message";
                    I18nResourceItem dialogI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, childComponentId, message, message);
                    this.addInCollection(i18nResourceItemCollection, dialogI18nResourceItem);
                }
                if(editors.containsKey("onLabel")&&editors.get("onLabel")!=null){
                    String onLabel= (String) editors.get("onLabel");

                    String childComponentId = generatedComponentId +   "/onLabel";
                    I18nResourceItem dialogI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, childComponentId, onLabel, onLabel);
                    this.addInCollection(i18nResourceItemCollection, dialogI18nResourceItem);
                }
                if(editors.containsKey("offLabel")&&editors.get("offLabel")!=null){
                    String offLabel= (String) editors.get("offLabel");

                    String childComponentId = generatedComponentId +   "/offLabel";
                    I18nResourceItem dialogI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, childComponentId, offLabel, offLabel);
                    this.addInCollection(i18nResourceItemCollection, dialogI18nResourceItem);
                }
            }

        }
        return i18nResourceItemCollection;
    }
    @Override
    protected I18nResourceItemCollection extractAttributeI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        return null;
    }
}
